package com.chx.three.mama.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.chx.three.mama.R;
import com.chx.three.mama.model.ActionList;
import com.chx.three.mama.utils.ImageLoadingHandle;

import java.util.List;

/**
 * Created by Hainuo on 2015/3/22.
 */
public class ActionAdapter extends BaseAdapter {
    private List<ActionList> data;

    private LayoutInflater inflater;

    public ActionAdapter(Context context, List<ActionList> data) {
        if (context != null) {
            inflater = LayoutInflater.from(context);
        } else {
            throw new IllegalArgumentException("Context must not null");
        }
        this.data = data;
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (data != null) {
            ret = data.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        Object ret = null;
        if (data != null) {
            ret = data.get(position);
        }
        return ret;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;

        if (convertView != null) {
            ret = convertView;
        } else {
            ret = inflater.inflate(R.layout.item_action_list, parent, false);
        }

        ViewHolder holder = (ViewHolder) ret.getTag();
        if (holder == null) {
            holder = new ViewHolder();
            holder.prizeImg = (ImageView) ret.findViewById(R.id.act_img);
            holder.prizeNum = (TextView) ret.findViewById(R.id.prize_num);
            holder.actionTime = (TextView) ret.findViewById(R.id.act_time);
            holder.timeOutImg = (ImageView) ret.findViewById(R.id.time_out_img);
            holder.txtTimeOut = (TextView) ret.findViewById(R.id.txt_act_time_out);

            ret.setTag(holder);
        }

        ActionList actionList = data.get(position);

        String prizeNum = actionList.getPrizeNum();
        prizeNum = prizeNum + "份";
        holder.prizeNum.setText(prizeNum);

        String actionTime = actionList.getOutTime();
        if (actionTime.equals("")) {
            holder.timeOutImg.setVisibility(View.VISIBLE);
            holder.txtTimeOut.setVisibility(View.INVISIBLE);
            holder.actionTime.setText("已结束");
        } else {
            holder.actionTime.setText(actionTime);
            holder.timeOutImg.setVisibility(View.INVISIBLE);
        }

        holder.prizeImg.setImageResource(R.drawable.act_default);

        String imgUrl = actionList.getPic();
        ImageLoadingHandle.imageHandle(imgUrl, holder.prizeImg);

        return ret;
    }

    private static class ViewHolder {
        private ImageView prizeImg;
        private TextView prizeNum;
        private TextView actionTime;
        private ImageView timeOutImg;
        private TextView txtTimeOut;
    }
}
